package com.atguigu.yygh.sms.service.impl;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.sms.service.SmsService;
import com.atguigu.yygh.sms.utils.HttpUtils;
import com.atguigu.yygh.vo.sms.SmsVo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2022/12/29 - 09:21
 * @Author: Hyoung
 * @Description:
 */

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Override
    public void send(SmsVo smsVo) {

        log.info("发送短信" + smsVo);
        String host = "http://dingxin.market.alicloudapi.com";
        String path = "/dx/sendSms";
        String method = "POST";
        String appcode = "67841ed3a5244407ac8b026ce7694d43";
        Map<String, String> headers = new HashMap<String, String>();
        //Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode); //appcode
        Map<String, String> querys = new HashMap<>();
        querys.put("mobile", smsVo.getPhone());//手机号
        Map<String, Object> param = smsVo.getParam();
        Set<Map.Entry<String, Object>> entries = param.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
            Object value = entry.getValue();
            querys.put("param", key + ":" + value);
        }
        querys.put("param", "code:" + querys); //参数列表
        querys.put("tpl_id", smsVo.getTemplateCode()); //短信验证码模板id
        Map<String, String> bodys = new HashMap<>();
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers,
                    querys, bodys);

            String data = EntityUtils.toString(response.getEntity());
            log.info(data);
            Gson gson = new Gson();
            HashMap<String, String> resultMap = gson.fromJson(data, HashMap.class);
            String returnCode = resultMap.get("return_code");

            if (!"00000".equals(returnCode)) {
                log.error("短信发送失败：return_code = " + returnCode);
                throw new YyghException(20011, "短信发送失败");
            }
        } catch (Exception e) {
            throw new YyghException(20011, "短信发送失败", e);
        }
    }

    //发送验证码
    @Override
    public void send(String phone, String code) {

        String host = "http://dingxin.market.alicloudapi.com";
        String path = "/dx/sendSms";
        String method = "POST";
        String appcode = "67841ed3a5244407ac8b026ce7694d43";
        Map<String, String> headers = new HashMap<String, String>();
        //Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode); //appcode
        Map<String, String> querys = new HashMap<>();
        querys.put("mobile", phone);//手机号
        querys.put("param", "code:" + code); //参数列表
        querys.put("tpl_id", "TP1711063"); //短信验证码模板id
        Map<String, String> bodys = new HashMap<>();
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers,
                    querys, bodys);

            String data = EntityUtils.toString(response.getEntity());
            log.info(data);
            Gson gson = new Gson();
            HashMap<String, String> resultMap = gson.fromJson(data, HashMap.class);
            String returnCode = resultMap.get("return_code");

            if (!"00000".equals(returnCode)) {
                log.error("短信发送失败：return_code = " + returnCode);
                throw new YyghException(20011, "短信发送失败");
            }
        } catch (Exception e) {
            throw new YyghException(20011, "短信发送失败", e);
        }
    }
}